package fourmiefight.ihm;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;

import fourmiefight.controller.Simulateur;

/**
 * @author M1G2
 *
 */
public class FenGenView implements ActionListener {
  
  private JFrame fenGen = new JFrame();
  private JMenuBar menuBar = new JMenuBar();
  private JMenu menu = new JMenu("Fichier");
  private JMenu param = new JMenu("Paramètres");
  private JMenuItem gereParam = new JMenuItem("Ouvrir paramètre");
  private JMenuItem close = new JMenuItem("Quitter", new ImageIcon("./Ressources/close.png"));
  private static Simulateur simu;
  private JPanel panelGlobal = new JPanel();
  private JPanel jp = new JPanel(new BorderLayout());
  private JPanel jpStat = new JPanel();
  private JPanel jpStatNorth = new JPanel();
  private JLabel jlStatNorth = new JLabel();
  private JPanel jpStatSouth = new JPanel();
  private JLabel jlStatSouth = new JLabel();
  private JPanel canvas = new JPanel();
  private simuState currentState;
private JButton jbPlay;
private JButton jbPause;
private JButton jbStop;
private JButton jbReset;
  
  protected enum simuState
  {
	  BEFORE_START,
	  STARTED,
	  PAUSED,
	  STOPPED,
	  FASTER
  }
  
  private void updateButtonsFromState(simuState newState)
  {
	  currentState = newState;
	  switch(currentState)
	  {
	  case BEFORE_START:
		  jbPlay.setEnabled(true);
		  jbPause.setEnabled(false);
		  jbStop.setEnabled(false);
		  jbReset.setEnabled(false);
		  break;
	  case PAUSED :
		  jbPlay.setEnabled(true);
		  jbPause.setEnabled(false);
		  jbStop.setEnabled(true);
		  jbReset.setEnabled(false);
		  break;
	  case STARTED :
		  jbPlay.setEnabled(false);
		  jbPause.setEnabled(true);
		  jbStop.setEnabled(true);
		  jbReset.setEnabled(false);
		  break;
	  case STOPPED :
		  jbPlay.setEnabled(true);
		  jbPause.setEnabled(false);
		  jbStop.setEnabled(false);
		  jbReset.setEnabled(true);
		  break;
	  case FASTER :
		  jbPlay.setEnabled(false);
		  jbPause.setEnabled(true);
		  jbStop.setEnabled(true);
		  jbReset.setEnabled(true);
	  default :
		break;  
	  }
  }
  
  public FenGenView() 
  {
	
	simu = new Simulateur(this);
    
	// Gestion dimension 
    Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
    Integer widthFen = 1024;
    Integer heightFen = 768;
 
    // Titre + icone 
    fenGen.setTitle("FourmisFight");
    fenGen.setIconImage(new ImageIcon("./Ressources/fight.gif").getImage());
    this.creerMenu();
    
    // Paramètrage fenêtre : taille, position et visibilité
    fenGen.setSize(widthFen, heightFen);
    
    fenGen.setLocation((int)((screenSize.getWidth()-widthFen)/2), 
        (int)((screenSize.getHeight()-heightFen)/2));
    
    panelGlobal.setLayout(new BorderLayout(3, 3));
    
    
    //********************* Ajout des élements
    // Panneau statistique
    jpStatNorth.setLayout(new BoxLayout(jpStatNorth, BoxLayout.PAGE_AXIS));
    jpStatNorth.add(new JLabel("<html><strong>Fourmillieres : </strong><br></html>"));
    jpStatNorth.add(jlStatNorth);
    jpStatNorth.setBorder(new EmptyBorder(0, 20, 0, 0));
    jpStatSouth.setLayout(new BoxLayout(jpStatSouth, BoxLayout.PAGE_AXIS));
    jpStatSouth.add(new JLabel("<html><strong>Nourritures : </strong><br></html>"));
    jpStatSouth.add(jlStatSouth);
    jpStatSouth.setBorder(new EmptyBorder(0, 20, 0, 0));
    
    jpStat.setLayout(new GridLayout(2, 1));
    jpStat.add(jpStatNorth);
    jpStat.add(jpStatSouth);
    
    jp.add(new JLabel("Statistiques"), BorderLayout.NORTH);
    jp.add(jpStat, BorderLayout.CENTER);
    jp.setPreferredSize(new Dimension(224, heightFen));
    
    
    
    
    JPanel buttonLine = new JPanel(new GridLayout(2, 4));
    
    jbPlay = new JButton("Play");
    jbPlay.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
        	updateButtonsFromState(simuState.STARTED);
        	simu.lancerSimu();
        }
    });
    buttonLine.add(jbPlay);
    
    jbPause = new JButton("Pause");
    jbPause.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
        	updateButtonsFromState(simuState.PAUSED);
        	simu.pauseSimu();
        }
    });
    buttonLine.add(jbPause);
    
    jbStop = new JButton("Stop");
    jbStop.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
        	updateButtonsFromState(simuState.STOPPED);
        	simu.stopSimu();
        }
    });
    buttonLine.add(jbStop);
    
    jbReset = new JButton("Reinit.");
    jbReset.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
        	updateButtonsFromState(simuState.BEFORE_START);
        	simu.resetSimu();
        }
    });
    buttonLine.add(jbReset);
    jp.add(buttonLine, BorderLayout.SOUTH);
    panelGlobal.add(jp, BorderLayout.WEST);
    
    canvas.add(simu.getSimuIhm().getCanvas());
    canvas.setPreferredSize(new Dimension(SimulateurIHM.getWidth(), SimulateurIHM.getHeight()));
    panelGlobal.add(canvas, BorderLayout.CENTER);
    
    
    // Propriété fenêtre
    fenGen.add(panelGlobal);
    fenGen.setVisible(true);
    fenGen.setResizable(false);
    fenGen.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
    updateButtonsFromState(simuState.BEFORE_START);
    
  }

  public void creerMenu() {
    
	// Raccourcis
	close.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q, KeyEvent.CTRL_MASK, true));
	
    // Ajout items
    menu.add(close);
    
    param.add(gereParam);
    
    // Listener sur menu
    close.addActionListener(this);
    
    gereParam.addActionListener(this);
    
    // Ajout menus
    menuBar.add(menu);
    menuBar.add(param);
    
    // Ajout barre des menus
    fenGen.setJMenuBar(menuBar);
    
  }

	@Override
	public void actionPerformed(ActionEvent arg0) {

		if(arg0.getSource() == close) {
			System.exit(0);
			fenGen.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		}
		else if(arg0.getSource() == gereParam){
			new ParamView(simu.getCarte());
		}
		
	}
	
	public static void main(String[] args) 
	{
		try 
		{
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
	    } 
		catch (Exception e)
		{
            e.printStackTrace();
	    } 
		
		//Main window = new Main();
		//window.setVisible(true);
		new FenGenView();
		simu.start();
	}

	public JLabel getJlStatNorth() {
		return jlStatNorth;
	}

	public void setJlStatNorth(JLabel jlStatNorth) {
		this.jlStatNorth = jlStatNorth;
	}

	public JLabel getJlStatSouth() {
		return jlStatSouth;
	}

	public void setJlStatSouth(JLabel jlStatSouth) {
		this.jlStatSouth = jlStatSouth;
	}
	
  
}
